package cn.jiahua.hospital.service.impl;

import cn.jiahua.hospital.constants.Constants;
import cn.jiahua.hospital.constants.MathUtil;
import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.mapper.SmsLogMapper;
import cn.jiahua.hospital.pojo.SmsLog;
import cn.jiahua.hospital.service.SmsLogService;
import cn.jiahua.hospital.utils.RedisUtil;
import cn.jiahua.hospital.utils.SMSUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class SmsLogServiceImpl implements SmsLogService {

    @Resource
    private SmsLogMapper smsLogMapper;

    @Autowired
    private SMSUtil smsUtil;

    @Autowired
    private RedisUtil redisUtil;


    public SmsLog getSmsLogById(Long id) {
        return smsLogMapper.getSmsLogById(id);
    }

    public List<SmsLog> getSmsLogListByMap(Map<String, Object> param) {
        return smsLogMapper.getSmsLogListByMap(param);
    }

    public Integer getSmsLogCountByMap(Map<String, Object> param) {
        return smsLogMapper.getSmsLogCountByMap(param);
    }

    public Integer qdtxAddSmsLog(SmsLog smsLog) {
        smsLog.setCreatdTime(new Date());
        return smsLogMapper.insertSmsLog(smsLog);
    }

    public Integer qdtxModifySmsLog(SmsLog smsLog) {
        smsLog.setUpdatedTime(new Date());
        return smsLogMapper.updateSmsLog(smsLog);
    }

    public Integer qdtxDeleteSmsLogById(Long id) {
        return smsLogMapper.deleteSmsLogById(id);
    }

    public Integer qdtxBatchDeleteSmsLog(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return smsLogMapper.batchDeleteSmsLog(param);
    }

    public Page<SmsLog> querySmsLogPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = smsLogMapper.getSmsLogCountByMap(param);
        pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SmsLog> smsLogList = smsLogMapper.getSmsLogListByMap(param);
        page.setList(smsLogList);
        return page;
    }


    @Override
    public SendSmsResponseBody sendMessage(String phone, Integer codeType) throws Exception {
        //1.调用短信接口发送短信信息
        String code = MathUtil.randomCode();
        System.out.println("======================" + code);
        SendSmsResponse sendSmsResponse = smsUtil.sendSms(phone, "{code:" + code + "}");

        //2.保存短信的发送信息

        if (sendSmsResponse != null && sendSmsResponse.getBody().getCode().equals("OK")) {
            //短信发送成功
            //3保存验证码并且保存有效期
            String key = Constants.Redis.PREFIX_SMS + codeType + ":" + phone;

            //4.把验证码放在redis中、
            redisUtil.set(key, code, Constants.Duration.MINUTE_INT * 5);
            SmsLog smsLog = new SmsLog();
            smsLog.setPhone(phone);
            smsLog.setCodeType(codeType);
            smsLog.setMessage(code);
            smsLog.setStatus(1);
            smsLog.setFailInfo("OK");
            smsLog.setCreatedUserId(Long.parseLong("0"));
            smsLog.setCreatdTime(new Date());
            smsLog.setUpdatedTime(new Date());
            smsLog.setIsDelete(0);
            try {
                smsLogMapper.insertSmsLog(smsLog);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return sendSmsResponse.getBody();
    }


}
